﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2010-08-26 16:31:32Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Security;

namespace Outcoder
{
	/// <summary>
	/// A generic <see cref="WeakReference"/>, which references an object 
	/// while still allowing that object to be reclaimed by garbage collection.   
	/// </summary>   
	/// <typeparam name="T">The type of the object that is referenced.</typeparam>   
	[Serializable]
	public class WeakReference<T> where T : class
	{
		readonly WeakReference weakReference;

#if SILVERLIGHT || WINDOWSPHONE
		[SecuritySafeCritical]
		protected WeakReference()
		{
			/* Intentionally left blank. */
		}
#endif

		/// <summary>
		/// Initializes a new instance of the <see cref="WeakReference&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="target">The target.</param>
		public WeakReference(T target)// : base(target)
		{
			weakReference = new WeakReference(target);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="WeakReference&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="target">The target.</param>
		/// <param name="trackResurrection">if set to <c>true</c> [track resurrection].</param>
		public WeakReference(T target, bool trackResurrection)
			//: base(target, trackResurrection)
		{
			weakReference = new WeakReference(target, trackResurrection);
		}

		/// <summary>
		/// Gets or sets the object (the target) referenced by the current 
		/// <see cref="T:System.WeakReference"/> object.
		/// </summary>
		/// <value></value>
		/// <returns>null if the object referenced by the current 
		/// <see cref="T:System.WeakReference"/> object has been garbage collected; 
		/// otherwise, a reference to the object referenced by the current 
		/// <see cref="T:System.WeakReference"/> object.
		/// </returns>
		/// <exception cref="T:System.InvalidOperationException">
		/// The reference to the target object is invalid. 
		/// This exception can be thrown while setting this property 
		/// if the value is a null reference or if the object 
		/// has been finalized during the set operation.
		/// </exception>
		public T Target
		{
			get
			{
				return (T)weakReference.Target;
			}
			set
			{
				weakReference.Target = value;
			}
		}

		/// <summary>
		/// Performs an implicit conversion 
		/// from <see cref="WeakReference{T}"/> 
		/// to <see cref="T"/>.
		/// </summary>
		/// <param name="reference">The reference.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator T(WeakReference<T> reference)
		{
			return reference != null ? reference.Target : null;
		}

		/// <summary>
		/// Performs an implicit conversion from <see cref="T"/> 
		/// to <see cref="WeakReference{T}"/>.
		/// </summary>
		/// <param name="target">The target.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator WeakReference<T>(T target)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			return new WeakReference<T>(target);
		}
	}
}
